package com.tansun.easycare.rule.ruleflow.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.ResMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.ruleflow.model.RuleFlowModel;
import com.tansun.easycare.rule.ruleflow.service.RuleFlowService;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.rule.common.RuleModelTypeEnum;
import com.tansun.rule.utils.StringUtils;

/**
 * 规则流 控制器
 *
 * @author ZengWenfeng
 */
@Controller
@RequestMapping(value = "/ruleProj/strategyFlow")
public class StrategyFlowController extends RuleBaseController {
    @Autowired
    private RuleFlowService ruleFlowService;
    @Autowired
    private RuleManageService ruleManageService;

    /**
     * 跳转index页面
     *
     * @param projectCode 项目编码
     * @param map         ModelMap
     * @return String 页面地址
     */
    @RequestMapping("index")
    public String index(HttpServletRequest request, HttpServletResponse response, String projectCode, RuleManageVO param, ModelMap map) throws Exception {
        param.setProjectCode(this.getCurrProjectCode());
        param.setModelType(RuleModelTypeEnum.StrategyFlow.getCode());
        PageInfo<RuleManageVO> page = this.ruleManageService.findRuleManageVO(param.getPage(), param);
        map.put("page", page);
        map.put("paramProjectCode", this.getCurrProjectCode());
        return "rule/strategyflow/index";
    }

    /**
     * 以新增模式打开流程设计器
     *
     * @param model 规则流Model对象
     * @param map   ModelMap
     * @return String 页面地址
     */
    @RequestMapping("openRuleFlowDetail")
    public String openRuleFlowDetail(HttpServletRequest request, HttpServletResponse response, @ModelAttribute RuleFlowModel model, ModelMap map) throws Exception {
        model.setProjectCode(this.getCurrProjectCode());
        //ruleFlowService.getRuleFlowData(model, map);
        RuleManage manage = ruleManageService.getById(model.getManageId());
        map.put("ruleflow", manage);
        return "rule/strategyflow/strategyflow";
    }
    /**
     * 以查看模式打开流程设计器
     *
     * @param model 规则流Model对象
     * @param map   ModelMap
     * @return String 页面地址
     */
    @RequestMapping("openViewDlg")
    public String openViewDlg(HttpServletRequest request, HttpServletResponse response, String code,String version, ModelMap map, String temp) throws Exception {
//		ruleFlowService.openViewDlg(model, map);
    	RuleManage manage = ruleManageService.getById(code);
    	if(manage==null){
    		manage = ruleManageService.findByCode(code, this.getCurrProjectCode());
    	}
    	RuleFlowModel model = new RuleFlowModel();
    	model.setManageId(manage.getId());
    	model.setProjectCode(manage.getProjectCode());
    	if(StringUtils.isNoneBlank(version)){
    		model.setVersion(version);
    	}else{
    		model.setVersion(manage.getVersion());
    		if("1".equals(temp)) {
    			model.setVersion(manage.getActiveVersion());
    		}
    	}
        //ruleFlowService.getRuleFlowData(model, map);
        map.put("ruleflow", manage);
        return "rule/strategyflow/strategyflow";
    }
    /**
     * 新增规则流
     *
     * @param model 规则流Model对象
     * @return ResMessage 消息对象
     */
    @RequestMapping("saveOrUpdate")
    public @ResponseBody
    ResMessage saveOrUpdate(@RequestBody RuleFlowModel model) throws Exception {
        ResMessage message = new ResMessage();
        try {
        	model.setModelType(RuleModelTypeEnum.StrategyFlow.getCode());
            ruleFlowService.saveOrUpdate(model);
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg(e.getMessage());
            message.setCode("failure");
        }
        return message;
    }
  
    @Override
    public void setRuleBizService() {
        // TODO Auto-generated method stub
        super.setRuleBaseService(this.ruleFlowService);
    }
}